using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    class clsTAU
    {
        public clsTAU(string sConn)
	    {
		    SqlHelper.ConnectionString = sConn;
	    }
        public List<TAU> DataTableToLstObj(DataTable dt)
        {
            TAU tauDTO;
            List<TAU> lstTauDTO = new List<TAU>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                tauDTO = new TAU();
                try
                {
                    tauDTO._maTau = dt.Rows[i]["MaTau"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    tauDTO._tenTau = dt.Rows[i]["TenTau"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    tauDTO._tenTau2KiTu = dt.Rows[i]["TenTau2KiTu"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    tauDTO._plantReferenceSAP = dt.Rows[i]["PlantReferenceSAP"].ToString();
                }
                catch (Exception ex)
                {
                }
                lstTauDTO.Add(tauDTO);
            }
            return lstTauDTO;
        }
        public TAU DataTableToObj(DataTable dt)
        {
            TAU tauDTO = new TAU();
            try
            {
                tauDTO._maTau = dt.Rows[0]["MaTau"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                tauDTO._tenTau = dt.Rows[0]["TenTau"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                tauDTO._tenTau2KiTu = dt.Rows[0]["TenTau2KiTu"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                tauDTO._plantReferenceSAP = dt.Rows[0]["PlantReferenceSAP"].ToString();
            }
            catch (Exception ex)
            {
            }
            return tauDTO;
        }
        public List<TAU> LoadTau()
        {
            DataTable dt = new DataTable();
            List<TAU> lstTauDTO = new List<TAU>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectTAUsAll";
                dt = SqlHelper.ExecuteToDataTable(spName, CommandType.StoredProcedure);
                lstTauDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstTauDTO;
        }
        public TAU LoadTauByMaTau(string maTau)
        {
            DataTable dt = new DataTable();
            TAU tDTO = new TAU();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectTAU";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaTau", maTau);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                tDTO = DataTableToObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return tDTO;
        }
    }
}
